package com.stjb.furns.filter;

import com.stjb.furns.utils.JDBCUtilsByDruid;

import javax.servlet.*;
import java.io.IOException;

/**
 * @author situ
 * @version 1.0
 * @date 2025/5/31 16:31
 * @Description: 管理事务过滤器
 */
public class TransactionFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {


        try {
            //放行
            filterChain.doFilter(servletRequest,servletResponse);
            //
            JDBCUtilsByDruid.commit();//提交事务
        } catch (Exception e) {
            JDBCUtilsByDruid.rollback();//出现异常回滚
            //抛出异常到tomcat tomcat根据errorpage来显示错误页面
            throw new RuntimeException(e);
            // e.printStackTrace();
        }
    }

    @Override
    public void destroy() {

    }
}
